package com.wswy.wzcx.ui.coupon

import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.che.libcommon.ui.adapter.SampleRecyclerViewAdapter
import com.wswy.wzcx.R
import com.wswy.wzcx.model.coupon.CouponModel
import com.wswy.wzcx.model.home.RPaths
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.base.loadLayout

class CouponListAdapter(private val needSelect:Boolean = false) : SampleRecyclerViewAdapter<CouponModel?,CouponListAdapter.CouponViewHolder>() {

    init {

        setOnItemClickListener { itemView, position, t ->


            if (needSelect) {

                if (selectedPosition == position) {
                    //取消
                    selectedPosition = RecyclerView.NO_POSITION
                    notifyItemChanged(position)
                } else {

                    if (selectedPosition != RecyclerView.NO_POSITION) {
                        //刷新上一个
                        notifyItemChanged(selectedPosition)
                    }
                    selectedPosition = position
                    notifyItemChanged(selectedPosition)
                }

            }else{

                //点击使用
                // 没有区分优惠券类型！！！

                RouterUtils.start(itemView.context,RPaths.fkdj)

            }
        }
    }

    private var selectedPosition = RecyclerView.NO_POSITION

    fun setDefaultSelectedPosition(position: Int){
        selectedPosition = position
    }

    fun getSelectedData() = getItemData(selectedPosition)

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): CouponViewHolder {
        return CouponViewHolder(loadLayout(parent, R.layout.item_coupon_list),needSelect)
    }

    override fun onBindViewHolder(holder: CouponViewHolder, position: Int, data: CouponModel?) {

        data?.run {

            val context = holder.itemView.context


            holder.tvAmount.text = SpannableString(context.getString(R.string.coupon_amount_args,amountStr)).apply {

                setSpan(RelativeSizeSpan(0.8f),0,1,SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)

                setSpan(StyleSpan(Typeface.BOLD),1,length,SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
            }

            holder.tvTitle.text = title

            holder.tvExpired.text = context.getString(R.string.coupon_expired,expiredStr)

            holder.imgSelect.isSelected = selectedPosition == position

        }

    }


    class CouponViewHolder(itemView:View, needSelect:Boolean = false) : RecyclerView.ViewHolder(itemView) {
        val rlLeftAmount : View = itemView.findViewById(R.id.rl_amount)
        val rlRightInfo : View = itemView.findViewById(R.id.rl_info)

        val tvTitle : TextView = itemView.findViewById(R.id.tv_title)
        val tvAmount : TextView = itemView.findViewById(R.id.tv_amount)
        val tvExpired : TextView = itemView.findViewById(R.id.tv_expired)

        val imgSelect : View = itemView.findViewById(R.id.img_check)

        val tvUsage : TextView = itemView.findViewById(R.id.tv_usage)

        init {
            val radius = SizeUtils.dp2px(6f).toFloat()
            rlLeftAmount.background = InsideCornersDrawable(ContextCompat.getDrawable(itemView.context, R.drawable.coupon_left_bg)).apply {
                topRightRadius = radius
                bottomRightRadius = radius
            }

            rlRightInfo.background = InsideCornersDrawable(ColorDrawable(Color.WHITE)).apply {
                topLeftRadius = radius
                bottomLeftRadius = radius
            }

            if (needSelect){
                tvUsage.visibility = View.GONE
                imgSelect.visibility = View.VISIBLE
            }else{
                tvUsage.visibility = View.VISIBLE
                imgSelect.visibility = View.GONE
            }

        }
    }
}